//
//  AIWalletController.m
//  Mia
//
//  Created by Vill on 2024/1/12.
//

#import "AIWalletController.h"
#import "AIHomeWalletBtn.h"    // 钱包按钮
#import "AIWalletListViewCell.h"
#import "AIGradientTabView.h"
#import "AIWalletTitleView.h"  // 标题

@interface AIWalletController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIVisualEffectView *blurView; //模糊
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) AIHomeWalletBtn *walletBtn;  //钱包按钮
@property (nonatomic, strong) NSMutableArray *walletArr;   //金币数
@property (nonatomic, strong) AIGradientTabView *tableView;
@property (nonatomic, strong) AIWalletTitleView *titView;  //标题

@end

@implementation AIWalletController

- (NSMutableArray *)walletArr{
    if(!_walletArr){
        _walletArr = [NSMutableArray array];
    }
    return _walletArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 模糊
    [self.view addSubview:self.blurView];
    [self.blurView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    // 关闭
    [self.view addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(34));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(12.5));
        make.top.mas_equalTo(self.view).mas_offset(KStatusBarHeight + PTW(5));
    }];
    
    // 钱包
    [self.view addSubview:self.walletBtn];
    [self.walletBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_greaterThanOrEqualTo(PTW(60));
        make.height.mas_equalTo(PTW(34));
        make.centerY.mas_equalTo(self.closeBtn);
        make.leading.mas_equalTo(self.view).mas_offset(PTW(12.5));
    }];
    AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
    [self.walletBtn setWallet:model.coinBalance];
    
    [self.view addSubview:self.tableView];
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.bottom.mas_equalTo(self.view);
//        make.top.mas_equalTo(self.view).mas_offset(KNavigationBarHeight);
//    }];
    [self loadGoodsList];
}

// 获取金币列表
-(void)loadGoodsList{
    [MBProgressTool mb_showHUDInView:self.view];
    [AIManager requestMethod:@"POST" url:api_goodsCoinList parameters:@{} successBlock:^(id  _Nullable result) {
        [MBProgressTool mb_hideHUDInView:self.view];
        [self.walletArr removeAllObjects];
        self.walletArr = [AIWalletModel mj_objectArrayWithKeyValuesArray:result];
        NSLog(@"result ------ %@",result);
        [self.tableView reloadData];
    } failedBlock:^(NSError * _Nullable error) {
        [MBProgressTool mb_hideHUDInView:self.view];
    } showHUD:NO];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.walletArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AIWalletListViewCell *cell = [AIWalletListViewCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.walletModel = self.walletArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return PTW(90);
}

#pragma mark --------------- lazy ---------------
- (UIVisualEffectView *)blurView{
    if(!_blurView){
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        _blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        _blurView.alpha = 1;
        _blurView.backgroundColor = [UIColor colorHex:@"#AAAAAA" alpha:0.3];
    }
    return _blurView;
}

- (UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:KImaged(@"ai_main_close") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

-(void)closeAction{
    [self dissMissAction];
    if (self.closeBlock){
        self.closeBlock();
    }
}

// 钱包
- (AIHomeWalletBtn *)walletBtn{
    if(!_walletBtn){
        _walletBtn = [AIHomeWalletBtn new];
    }
    return _walletBtn;
}

- (AIGradientTabView *)tableView{
    if(!_tableView){
        _tableView = [[AIGradientTabView alloc] initWithFrame:CGRectMake(0, KNavigationBarHeight, KScreenWidth, KScreenHeight - KNavigationBarHeight) direction:AIGradualDirectionTop gradualValue:@.2f];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.titView;
    }
    return _tableView;
}

- (AIWalletTitleView *)titView{
    if(!_titView){
        _titView = [[AIWalletTitleView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, PTW(80))];
        _titView.titleLbl.text = @"Wallet";
    }
    return _titView;
}

@end
